package com.qdu.dao;

import com.qdu.entity.ActiSingUp;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *根据活动编号确定参加人员，
 * @author 19645
 */
public class ActiSingUpDao {
    
    //我的活动，根据用户id获取参与的活动
    //这里或者可以写成返回的是一个用户列表，但是丁紫依用户列表还没有完成，先返回列表。
    public List<ActiSingUp> getActivByAid(String Uid){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<ActiSingUp> list = new ArrayList<>();
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Aid,Stime from SignUp where Uid=?");
            ps.setString(1, Uid);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new ActiSingUp(
                        rs.getString(1),
                        rs.getString(2)                        
                )
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }
        
    //我要报名
    public int SignUp(String Aid,String Uid){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int rows=0;
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("insert SignUp values(?,?,getdate(),1)");
            ps.setString(1, Uid);
            ps.setString(2,Aid);
            rows = ps.executeUpdate();
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return rows;
    }
    
    //检查此人之前是否报过名
    public ActiSingUp IfSigned(String Uid,String Aid){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Uid,Aid,Stime from SignUp where Uid=? and Aid=?");
            ps.setString(1, Uid);
            ps.setString(2,Aid);
            rs = ps.executeQuery();
            while (rs.next()) {
                return new ActiSingUp(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getString(3)
                        );
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return null;
    }
    
}

